package com.aat.dxfy.proxy.t01;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;

public class Test01 {
	public static void main(String[] args) {
		demo01();
		demo02();
	}

	public static void demo01() {
		Foo f=new FooImpl();
		
		InvocationHandler handler = new MyInvocationHandler(f);
		
		Foo foo = (Foo) Proxy.newProxyInstance(handler.getClass().getClassLoader(),
                f.getClass().getInterfaces(),
                handler);
		System.out.println(foo.getClass().getName());
		foo.test("123");
	}

	public static void demo02() {
		Foo f=new FooImpl();
		InvocationHandler handler = new MyInvocationHandler(f);
		Class<?> proxyClass = Proxy.getProxyClass(handler.getClass().getClassLoader(),f.getClass().getInterfaces());
		try {
			Foo foo = (Foo) proxyClass.getConstructor(InvocationHandler.class).newInstance(handler);
			foo.test("456");
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
	}
}

interface Foo {
	public void test(String s) ;
}

class FooImpl implements Foo{

	@Override
	public void test(String s) {
		System.out.println(s);
	}
	
}